package com.itheima.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.UserMapper;
import com.itheima.pojo.User;
import com.itheima.utills.SessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Author Azer
 * @LOVE,,,
 * @Date 2021/9/22 20:43
 */
public class UserService {

    /**
     * 根据关键字搜索用户数据
     * @return
     */
    public List<User> searchUserByKeywords(String keyword){
        SqlSession sqlSession = SessionFactoryUtils.getSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        List<User> users = userMapper.searchUserByKeywords(keyword);
        sqlSession.close();

        return users;
    }


    /**
     * 删除用户
     * @param id
     * @return
     */
    public boolean deleteUserById(int id) {

        SqlSession sqlSession = SessionFactoryUtils.getSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        int i = userMapper.deleteUserById(id);
        if(i>0){
            sqlSession.commit();
            sqlSession.close();
            return true;
        }else {
            sqlSession.rollback();
            sqlSession.close();
            return false;
        }
    }

    /**
     * （修改用户）
     * 根据id用户 用来回显数据
     * @param id
     * @return
     */
    public User findUserById(int id){
        SqlSession sqlSession = SessionFactoryUtils.getSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        User user = userMapper.findUserById(id);
        return user;
    }

    /**
     * 修改用户
     */
    public boolean updateUser(User user){
        //对User对象中的数据进行合法性验证
        if (user == null ||
                user.getName() == null || user.getName().length()==0||
                user.getEmail() == null || user.getEmail().length()==0) {
            return false;
        }

        SqlSession sqlSession = SessionFactoryUtils.getSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        userMapper.updateUser(user);

        sqlSession.commit();

        sqlSession.close();

        return true;
    }

    /**
     * 添加用户  把用户在浏览器页面提交的信息添加到数据库
     * 细节处理，根据邮箱是否注册来添加新用户
     *
     * @return
     */
    public boolean addUser(User user) {
        //对User对象中的数据进行合法性验证
        if (user == null ||
                user.getName() == null || user.getName().length()==0||
                user.getEmail() == null || user.getEmail().length()==0) {
            return false;
        }

        SqlSession sqlSession = SessionFactoryUtils.getSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        //查询邮箱是否被注册
        User userByEmail = userMapper.findUserByEmail(user.getEmail());
        if (userByEmail != null) {
            //邮箱已被注册
            sqlSession.close();
            return false;
        }else {
            //可以添加用户
            int i = userMapper.addUser(user);
            if (i > 0) {
                //添加成功 提交
                sqlSession.commit();
                sqlSession.close();
            } else {
                //添加失败 事务回滚
                sqlSession.rollback();
            }

            return true;
        }
    }

    /**
     * 使用PageHelper实现分页查询
     *
     * @param currentPage
     * @return
     */
    public PageInfo<User> findUserByPage(int currentPage) {

        //每页显示的数据条数
        int pageSize = 3;


        SqlSession sqlSession = SessionFactoryUtils.getSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);



        //设置分页查询
        PageHelper.startPage(currentPage, pageSize);

        //查询数据
        List<User> users = userMapper.findAllUser();

        //释放资源
        sqlSession.close();

        //实例化PageInfo对象
        PageInfo<User> pageInfo = new PageInfo<>(users);

        return pageInfo;


    }
}
